package com.ext.aic.utils;

import android.app.Activity;
import android.view.View;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.ext.aic.R;
import com.ext.aic.beans.OptionSingleItem;
import com.ext.aic.enums.Gender;
import com.eyinfo.android_pure_utils.events.Action1;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class PickerUtils {

    private static volatile PickerUtils instance;

    public static PickerUtils getInstance() {
        if (instance == null) {
            synchronized (PickerUtils.class) {
                if (instance == null) {
                    instance = new PickerUtils();
                }
            }
        }
        return instance;
    }

    private static class OptionsSelected<T> implements OnOptionsSelectListener {

        private List<T> items;

        private Action1<T> call;

        public OptionsSelected(List<T> items, Action1<T> call) {
            this.items = items;
            this.call = call;
        }

        @Override
        public void onOptionsSelect(int position, int position2, int position3, View v) {
            if (Objects.isNull(items) || call == null) {
                return;
            }
            call.call(items.get(position));
        }
    }

    private <T> void show(Activity activity, List<T> items, Action1<T> call) {
        OptionsPickerView<T> build = new OptionsPickerBuilder(activity, new OptionsSelected(items, call))
                .setSubmitText(activity.getString(R.string.confirm))
                .setCancelText(activity.getString(R.string.cancel))
                .setTitleText(activity.getString(R.string.select_gender))
                .setSubCalSize(16)
                .setTitleSize(14)
                .setTitleColor(0xff1f1f1f)
                .setSubmitColor(0xff1f1f1f)
                .setCancelColor(0xff1f1f1f)
                .setTitleBgColor(0xfff6f6f6)
                .setBgColor(0xffffffff)
                .setContentTextSize(16)
                .setSelectOptions(1)
                .setOutSideCancelable(true)
                .isDialog(false)
                .isRestoreItem(true)
                .setDividerColor(0x4dd5d5d5)
                .build();
        build.setPicker(items);
        build.show();
    }

    public void showGender(Activity activity, Action1<OptionSingleItem> call) {
        List<OptionSingleItem> items = new ArrayList<>();
        items.add(new OptionSingleItem(Gender.MALE.getLabel(), Gender.MALE.getValue()));
        items.add(new OptionSingleItem(Gender.FEMALE.getLabel(), Gender.FEMALE.getValue()));
        show(activity, items, call);
    }
}
